#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "systemframebuffer.h"
#include "lcd_font.h"
#include "define_comm.h"

#define kFB_WIDTH 128
#define kFB_PAGES 8
#define kASII_WIDTH  8
#define kGB21_WIDTH  16
#define WORDSIZE 3

#define LCD_PIC_Y 1
#define LCD_PIC_X 24

sj_int32 JzDisPlay_Char(int16_t x,int16_t page,int16_t num,int16_t mode, uint8_t data_temp[])
{
    uint8_t (*gs_FrameBuffer)[128] = data_temp;
    sj_int16 i=0,index=0;
    sj_int8 t1,t2;
    if((num<' ')||(num>'~'))
        return 0;
    num = num -' '; //字符在数组中的位置
    for(i=0; i<kASII_WIDTH; i++){
        index =i<<1;
        if(mode == 1)
        {
            t1 = g_ASII_1608[num][index]^0xff;
            t2 = g_ASII_1608[num][index+1]^0xff;
        }else
        {
            t1 = g_ASII_1608[num][index];
            t2 = g_ASII_1608[num][index+1];
        }
        gs_FrameBuffer[page][x+i] = t1;
        if(page<7){ 	//page为7的话，就不能再加了
            gs_FrameBuffer[page+1][x+i] = t2;
        }
    }
    return kASII_WIDTH;
}

sj_int32 JzDisPlay_Str(const uint8_t *buf,int16_t page,int16_t x, uint8_t data_temp[]) //单纯显示内容的时候调用的这个，比如显示SIM卡号，版本信息等
{
    uint8_t size=0;
    sj_uint16 sum=0;
    while((*buf<='~')&&(*buf>=' '))//判断是不是非法字符!
    {
        if(x>kFB_WIDTH)
            break;
        size = JzDisPlay_Char(x,page,*buf,0, data_temp);//显示字符
        x+=size;
        buf++;
        sum+=1;
    }
    return 	sum;
}

sj_int32 JzDisPlay_Char_0_10(int16_t x,int16_t page,int16_t num,int16_t mode, uint8_t data_temp[])
{
    uint8_t (*gs_FrameBuffer)[128] = data_temp;
    sj_uint16 i=0,index=0;
    char t1,t2;
    if((num<' ')||(num>'~'))//判断非法字符
        return 0;
    num =num -' ';
    for(i=0; i<kASII_WIDTH; i++){
        index =i<<1;
        t1 = g_ASII_0_10[num][index];
        t2 = g_ASII_0_10[num][index+1];

        gs_FrameBuffer[page][x+i] = t1;
        if(page<7){
            gs_FrameBuffer[page+1][x+i] = t2;
        }
    }
    return kASII_WIDTH;
}

sj_int32 sj_lcd_show_date(const uint8_t *buf,int16_t page,int16_t x, uint8_t data_temp[]) //显示时间的时候用的这个函数 年-月-日
{
    uint8_t size=0;
    sj_uint16 sum=0;
    while((*buf<='~')&&(*buf>=' '))//判断是不是非法字符!
    {
        if(x>kFB_WIDTH)
            break;
        size = JzDisPlay_Char_0_10(x,page,*buf,0, data_temp);
        x+=size;
        buf++;
        sum+=1;
    }
    return 	sum;
}

sj_void sj_lcd_show_pic(const uint8_t *buf,int16_t page,int16_t x,int16_t size,int16_t mode, uint8_t data_temp[])
{
    uint8_t (*gs_FrameBuffer)[128] = data_temp;
    sj_uint16 i=0,index=0;
    sj_uint8 t1,t2;
    for(i=0; i<size; i++){
        index =i<<1;
        if(mode == 1)
        {
            t1 = buf[index]^0xff;
            t2 = buf[index+1]^0xff;
        }else{
            t1 = buf[index];
            t2 = buf[index+1];
        }
        gs_FrameBuffer[page][x+i] = t1;
        if(page<7)
        {
            gs_FrameBuffer[page+1][x+i] = t2;
        }
    }
    return 0;
}

//sj_void lcd_p16x32str(uint8_t x,uint8_t y,uint8_t ch[], uint8_t data_temp[]) //显示时速的单位‘ km/h ’
sj_void sj_lcd_speed_show(uint8_t x,uint8_t y,uint8_t ch[], uint8_t data_temp[])
{
    uint8_t (*gs_FrameBuffer)[128] = data_temp;
    uint8_t j=0;
    sj_uint16 i=0,index=0;
    while (ch[j]!='\0')
    {
         char num =ch[j]-'0'; //字符在数组中的位置
         switch(ch[j]){
             case 'k':
                 num = 10;
                 break;
             case 'm':
                 num = 11;
                 break;
             case '/':
                 num = 12;
                 break;
             case 'h':
                 num = 13;
                 break;
         }
         index = 0;
     if(x>120){x=0;y++;}
         for(i=0;i<16;i++)
         {
             gs_FrameBuffer[y][x+i] = F16x32[num][index++];
             gs_FrameBuffer[y+1][x+i] = F16x32[num][index++];
             gs_FrameBuffer[y+2][x+i] = F16x32[num][index++];
             gs_FrameBuffer[y+3][x+i] = F16x32[num][index++];
         }
     x+=16;
     j++;
    }
}

//输出字母
//sj_void lcd_p8x16str(uint8_t x,uint8_t y,uint8_t ch[],int8_t mode, uint8_t data_temp[])
sj_void sj_lcd_english_show(uint8_t x,uint8_t y,uint8_t ch[],int8_t mode, uint8_t data_temp[])
{
    uint8_t (*gs_FrameBuffer)[128] = data_temp;
    sj_int16 i, index;
    sj_int8 word_pos = 0, pos = 0;
    while(ch[pos] != '\0')
    {
        word_pos = ch[pos] - 32; //获取字母在数组中的位置
        if(x > 120)
        {
            x = 0;
            y++;
        }
        for(i = 0; i < kASII_WIDTH; i++)
        {
            index = i<<1;
            if(mode == 1)
            {
                gs_FrameBuffer[y][x+i] = g_ASII_1608[word_pos][index]^0xff;
                gs_FrameBuffer[y+1][x+i] = g_ASII_1608[word_pos][index+1]^0xff;
            }
            else
            {
                gs_FrameBuffer[y][x+i] = g_ASII_1608[word_pos][index];
                gs_FrameBuffer[y+1][x+i] = g_ASII_1608[word_pos][index+1];
            }
        }
        pos++;
        x += kASII_WIDTH;
    }
}

//输出汉字
//sj_void lcd_p16x16str(uint8_t x,uint8_t y,uint8_t ch[],int8_t mode, uint8_t data_temp[])
sj_void sj_lcd_chinese_show(uint8_t x,uint8_t y,uint8_t ch[],int8_t mode, uint8_t data_temp[])
{
    uint8_t (*gs_FrameBuffer)[128] = data_temp;
    sj_int32 word_pos = 0, adder = 1;
    sj_int32 i, pos = 0, index;
    while(ch[pos] != '\0')
    {
        i = 0;
        while(F16x16_Idx[word_pos] > 127)
        {
            if(ch[pos] == F16x16_Idx[word_pos]);
            {
                if(ch[pos+1] == F16x16_Idx[word_pos+1])
                {
                    if(ch[pos+2] == F16x16_Idx[word_pos+2])
                    {
                        adder = word_pos / WORDSIZE; //获取汉字在数组中的位置
                        break;
                    }
                }
            }
            word_pos += WORDSIZE;
        }
        for(i=0; i < kGB21_WIDTH; i++)
        {
            index = i<<1;
            if(mode == 1)
            {
                gs_FrameBuffer[y][x+i] = F16x16[adder][index]^0xff;
                gs_FrameBuffer[y+1][x+i] = F16x16[adder][index+1]^0xff;
            }else{
                gs_FrameBuffer[y][x+i] = F16x16[adder][index];
                gs_FrameBuffer[y+1][x+i] = F16x16[adder][index+1];
            }
        }
        pos += WORDSIZE;
        x += kGB21_WIDTH;
    }
}

sj_void sj_show_desktop_pic(sj_int8 x, sj_int8 y, struct desktop_signal_value *val, uint8_t data_temp[])
{
    uint8_t (*gs_FrameBuffer)[128] = data_temp;
    if(val == NULL)
    {
        return ;
    }
    if(val->gps == 0)
    {
        sj_lcd_show_pic((const uint8_t*)&g_SpecialPic[0 + val->value_4g],LCD_PIC_Y,LCD_PIC_X*0,16,0, gs_FrameBuffer);
    }
    else if(val->gps == 1)
    {
        sj_lcd_show_pic((const uint8_t*)&g_SpecialPic[5 + val->value_4g],LCD_PIC_Y,LCD_PIC_X*0,16,0, gs_FrameBuffer);
    }
        sj_lcd_show_pic((const uint8_t*)&g_SpecialPic[10 + val->signal],LCD_PIC_Y,LCD_PIC_X*1,16,0, gs_FrameBuffer);
        sj_lcd_show_pic((const uint8_t*)&g_SpecialPic[12 + val->camera],LCD_PIC_Y,LCD_PIC_X*2,16,0, gs_FrameBuffer);
        sj_lcd_show_pic((const uint8_t*)&g_SpecialPic[16 + val->hard_disk],LCD_PIC_Y,LCD_PIC_X*3,16,0, gs_FrameBuffer);
        sj_lcd_show_pic((const uint8_t*)&g_SpecialPic[18 + val->sd_card],LCD_PIC_Y,LCD_PIC_X*4,16,0, gs_FrameBuffer);
}

//输出汉字和字符混合字符串 //mode 参数是决定有无背景
sj_void sj_lcd_print(uint8_t x, uint8_t y,uint8_t ch[],int8_t mode, uint8_t data_temp[])
{
    uint8_t temp[3];
    int i = 0;
    if(y < 0 || y > 7)
    {
        return -1;
    }

    while(ch[i] != '\0')
    {
        if(ch[i] > 127) //汉字
        {
            temp[0] = ch[i];
            temp[1] = ch[i+1];
            temp[2] = ch[i+2];
            temp[3] = '\0';
            sj_lcd_chinese_show(x , y, temp, mode, data_temp);	//显示汉字
            x += kGB21_WIDTH;
            i += WORDSIZE;
        }
        else            //字母
        {
            temp[0] = ch[i];
            temp[1] = '\0';
            sj_lcd_english_show(x , y, temp, mode, data_temp);	//显示字母
            x += kASII_WIDTH;
            i++;
        }
    }
}
